﻿@model QuanLySanXuat.Models.BOMFormModels
@{
    ViewBag.Title = "Create";
}
<script type="text/javascript" src="@Url.Content("~/Scripts/bootstrap-datepicker.js")"></script>
<link href="@Url.Content("~/Content/datepicker.css")" rel="stylesheet" />
<script type="text/javascript">
    $(document).ready(function () {
        $('#CheckValid').bootstrapValidator({
            message: 'This value is not valid',
            excluded: [':disabled'],
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                Name: {
                    validators: {
                        notEmpty: {
                            message: 'Tên không để trống'
                        }
                    }
                },
                Code: {
                    validators: {
                        notEmpty: {
                            message: 'Tên không để trống'
                        }
                    }
                }
            }
        }).on('success.form.bv', function (e) {
            // Prevent form submission
            e.preventDefault();

            // Get the form instance
            var $form = $(e.target);

            // Get the BootstrapValidator instance
            var bv = $form.data('bootstrapValidator');

            // Use Ajax to submit form data
            $.ajax({
                type: "POST",
                url: '@Url.Action("Save", "BOM")',
                async: false,
                cache: false,
                data: $('#CheckValid').serialize(),
                success: function (result) {
                    location.href = '@Url.Action("Index", "BOM")'
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    alert(xhr.status);
                    alert(xhr.responseText);
                    alert(thrownError);
                }
            });
        });
    });
</script>
<div id="breadcrumb">
    <div class="container">
        <div id="con_top">
            <div class="c_container">
                <h3>Thêm công thức</h3>
            </div>
        </div>
    </div>
</div>
@if (Model != null)
{ 
    <form class="form-horizontal" role="form" id="CheckValid">
        <input type="hidden" id="ID" name="ID" value="@Model.ID" />
        <div class="form-group">
            <label for="Code" class="col-sm-2 control-label">Mã</label>
            <div class="col-sm-6">
                <input type="text" class="form-control" id="Code" name="Code" placeholder="Code" value="@Model.Code">
            </div>
        </div>
        <div class="form-group">
            <label for="Name" class="col-sm-2 control-label">Tên</label>
            <div class="col-sm-6">
                <input type="text" class="form-control" id="Name" name="Name" placeholder="Name" value="@Model.Name">
            </div>
        </div>
        <div class="form-group">
            <label for="dtEfective" class="col-sm-2 control-label">Ngày tác động</label>
            <div class="col-sm-6">
                <div class='input-group date' id='dtEfective'>
                    <input type='text' class="form-control" />
                    <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>
                    </span>
                </div>

            </div>
        </div>
        <div class="form-group">
            <label for="ProductTypeID" class="col-sm-2 control-label">Sản phẩm</label>
            <div class="col-sm-6">
                @Html.DropDownList("ProductID", new SelectList(ViewData["Product"] as IEnumerable<Data.Models.Product>, "ID", "Name", Model.ProductID), "Chọn sản phẩm", new { @class = "form-control" })
            </div>
        </div>
        <div class="form-group">
            <label for="Color" class="col-sm-2 control-label">Màu sắc</label>
            <div class="col-sm-6">
                <input type="text" class="form-control" id="Color" name="Color" placeholder="Color" value="@Model.Color">
            </div>
        </div>
        <div class="form-group">
            <label for="Description" class="col-sm-2 control-label">Mô tả</label>
            <div class="col-sm-6">
                <textarea id="Description" name="Description" class="form-control" rows="5" placeholder="Mô tả">@Model.Description</textarea>
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-6">
                <div class="checkbox">
                    <label>
                        <input type="checkbox" id="Active" name="Active" value="@Model.Active" checked="@Model.Active">
                        Active
                    </label>
                </div>
            </div>
        </div>
        <div class="form-group">
            <div class="c_container col-sm-10 col-sm-offset-1">
                <div id="form_body">
                    <table id="tblSaleDetail">
                        <thead>
                            <tr>
                                <th style="width: 140px;"><span>Nguyên liệu</span></th>
                                <th style="width: 170px;"><span>Mô tả</span></th>
                                <th style="width: 80px;"><span>Số lượng</span></th>
                                <th style="width: 90px;"><span>Đơn vị</span></th>
                                <th style="width: 20px;"><span></span></th>
                            </tr>
                        </thead>
                        <tbody id="insertRow">
                            @if (Model.BOMDetailFormModels.Count() > 0)
                            {
                                foreach (var item in Model.BOMDetailFormModels)
                                {
                                <tr>
                                    <td>
                                        @Html.DropDownList("MaterialID", new SelectList(ViewData["Material"] as IEnumerable<Data.Models.Material>, "ID", "Name", item.MaterialID), "Chọn nguyên liệu", new { @class = "span2 col-sm-1 form-control materialchange" })
                                    </td>
                                    <td>
                                        <input type="text" id="Detail" name="Detail" class="txt1 description" value="@item.Description">
                                    </td>
                                    <td>
                                        <input type="text" id="Quantity" name="Quantity" class="numeric text-right qty checkvalid" value="@item.Quantity" >
                                    </td>
                                    <td>
                                        @Html.DropDownList("UnitID", new SelectList(ViewData["UOM"] as IEnumerable<Data.Models.UOM>, "ID", "Name", item.UnitID), "Chọn đơn vị", new { @class = "span2 col-sm-1 form-control" })
                                    </td>
                                    <td>
                                        <div class="remove" id="delRow" onclick="RemoveRow(this);"></div>
                                    </td>
                                </tr>
                                }
                            }
                            else
                            {
                                <tr>
                                    <td>
                                        @Html.DropDownList("MaterialID", new SelectList(ViewData["Material"] as IEnumerable<Data.Models.Material>, "ID", "Name"), "Chọn nguyên liệu", new { @class = "span2 col-sm-1 form-control materialchange" })
                                    </td>
                                    <td>
                                        <input type="text" id="Detail" name="Detail" class="txt1 description">
                                    </td>
                                    <td>
                                        <input type="text" id="Quantity" name="Quantity" class="numeric text-right qty checkvalid">
                                    </td>
                                    <td>
                                        @Html.DropDownList("UnitID", new SelectList(ViewData["UOM"] as IEnumerable<Data.Models.UOM>, "ID", "Name"), "Chọn đơn vị", new { @class = "span2 col-sm-1 form-control materialchange" })
                                    </td>
                                    <td>
                                        <div class="remove" id="delRow" onclick="RemoveRow(this);"></div>
                                    </td>
                                </tr>
                            }

                        </tbody>
                    </table>
                </div>
                <div class="tablefooter">
                    <div class="pos1">
                        <input type="button" id="addline_CreateSale" class="addline" value="Thêm công thức chi tiết">
                    </div>
                </div>
                <div class="cleardiv"></div>
                <div id="form_body">
                    <table id="tblAccesoryDetail">
                        <thead>
                            <tr>
                                <th style="width: 140px;"><span>Phụ kiện</span></th>
                                <th style="width: 80px;"><span>Số lượng</span></th>
                                <th style="width: 170px;"><span>Mô tả</span></th>
                                <th style="width: 90px;"><span>Đơn vị</span></th>
                                <th style="width: 80px;"><span>Giá</span></th>
                                <th style="width: 20px;"><span></span></th>
                            </tr>
                        </thead>
                        <tbody id="insertAccessRow">
                            @if (Model.BOMAccessorie_DetailsFormModels != null)
                            {
                                foreach (var item in Model.BOMAccessorie_DetailsFormModels)
                                {
                                <tr>
                                    <td>
                                        @Html.DropDownList("AccessorieID", new SelectList(ViewData["Accessory"] as IEnumerable<Data.Models.Accessory>, "ID", "Name", item.AccessorieID), "Chọn phụ kiện", new { @class = "span2 col-sm-1 form-control accesscorychange" })
                                    </td>
                                    <td>
                                        <input type="text" id="QuantityAcces" name="QuantityAcces" class="numeric text-right qty checkvalid" value="@item.Quantity" >
                                    </td>
                                    <td>
                                        <input type="text" id="DetailAcces" name="DetailAcces" class="txt1 description" value="@item.Description">
                                    </td>
                                    <td>
                                        @Html.DropDownList("UnitIDAcces", new SelectList(ViewData["UOM"] as IEnumerable<Data.Models.UOM>, "ID", "Name", item.UnitID), "Chọn đơn vị", new { @class = "span2 col-sm-1 form-control" })
                                    </td>
                                      <td>
                                        <input type="text" id="PriceAcces" name="PriceAcces" class="numeric text-right qty checkvalid" value="@item.Price" >
                                    </td>
                                    <td>
                                        <div class="remove" id="delRow" onclick="RemoveRow(this);"></div>
                                    </td>
                                </tr>
                                }
                            }
                            else
                            {
                                <tr>
                                    <td>
                                        @Html.DropDownList("AccessorieID", new SelectList(ViewData["Accessory"] as IEnumerable<Data.Models.Accessory>, "ID", "Name"), "Chọn phụ kiện", new { @class = "span2 col-sm-1 form-control accesscorychange" })
                                    </td>
                                    <td>
                                        <input type="text" id="QuantityAcces" name="QuantityAcces" class="numeric text-right qty checkvalid">
                                    </td>
                                   <td>
                                        <input type="text" id="DetailAcces" name="DetailAcces" class="txt1 description">
                                    </td> 
                                    <td>
                                        @Html.DropDownList("UnitIDAcces", new SelectList(ViewData["UOM"] as IEnumerable<Data.Models.UOM>, "ID", "Name"), "Chọn đơn vị", new { @class = "span2 col-sm-1 form-control accesscorychange" })
                                    </td>
                                     <td>
                                        <input type="text" id="PriceAcces" name="PriceAcces" class="numeric text-right qty checkvalid">
                                    </td>
                                    <td>
                                        <div class="remove" id="delRow" onclick="RemoveRow(this);"></div>
                                    </td>
                                </tr>
                            }

                        </tbody>
                    </table>
                </div>
                <div class="tablefooter">
                    <div class="pos1">
                        <input type="button" id="addline_CreateAccsess" class="addline" value="Thêm phụ kiện">
                    </div>
                </div>
                <div class="cleardiv"></div>
                <div id="form_body">
                    <table id="tblWorkerDetail">
                        <thead>
                            <tr>
                                <th style="width: 140px;"><span>Nhân công</span></th>
                                <th style="width: 80px;"><span>Số lượng</span></th>
                                <th style="width: 170px;"><span>Mô tả</span></th>
                                <th style="width: 90px;"><span>Đơn vị</span></th>
                                <th style="width: 20px;"><span></span></th>
                            </tr>
                        </thead>
                        <tbody id="insertWorkerRow">
                            @if (Model.BOMWorker_DetailsFormModels != null)
                            {
                                foreach (var item in Model.BOMWorker_DetailsFormModels)
                                {
                                <tr>
                                    <td>
                                        @Html.DropDownList("WorkerLevelID", new SelectList(ViewData["Worker"] as IEnumerable<Data.Models.WorkerLevel>, "ID", "Name", item.WorkerLevelID), "Chọn nhân công", new { @class = "span2 col-sm-1 form-control accesscorychange" })
                                    </td>
                                    <td>
                                        <input type="text" id="Quantity" name="Quantity" class="numeric text-right qty checkvalid" value="@item.Quantity" >
                                    </td>
                                    <td>
                                        <input type="text" id="Detail" name="Detail" class="txt1 description" value="@item.Description">
                                    </td>
                                    <td>
                                        @Html.DropDownList("UnitID", new SelectList(ViewData["UOM"] as IEnumerable<Data.Models.UOM>, "ID", "Name", item.UnitID), "Chọn đơn vị", new { @class = "span2 col-sm-1 form-control" })
                                    </td>
                                    <td>
                                        <div class="remove" id="delRow" onclick="RemoveRow(this);"></div>
                                    </td>
                                </tr>
                                }
                            }
                            else
                            {
                                <tr>
                                    <td>
                                        @Html.DropDownList("WorkerLevelID", new SelectList(ViewData["Worker"] as IEnumerable<Data.Models.WorkerLevel>, "ID", "Name"), "Chọn nhân công", new { @class = "span2 col-sm-1 form-control accesscorychange" })
                                    </td>
                                    <td>
                                        <input type="text" id="Quantity" name="Quantity" class="numeric text-right qty checkvalid">
                                    </td>
                                    <td>
                                        <input type="text" id="Detail" name="Detail" class="txt1 description">
                                    </td>
                                    <td>
                                        @Html.DropDownList("UnitID", new SelectList(ViewData["UOM"] as IEnumerable<Data.Models.UOM>, "ID", "Name"), "Chọn đơn vị", new { @class = "span2 col-sm-1 form-control accesscorychange" })
                                    </td>
                                    <td>
                                        <div class="remove" id="delRow" onclick="RemoveRow(this);"></div>
                                    </td>
                                </tr>
                            }

                        </tbody>
                    </table>
                </div>
                <div class="tablefooter">
                    <div class="pos1">
                        <input type="button" id="addline_CreateWorker" class="addline" value="Thêm nhân công">
                    </div>
                </div>
                <div class="cleardiv"></div>
                <div id="form_body">
                    <table id="tblMachineDetail">
                        <thead>
                            <tr>
                                <th style="width: 140px;"><span>Máy móc</span></th>
                                <th style="width: 80px;"><span>Số lượng</span></th>
                                <th style="width: 170px;"><span>Mô tả</span></th>
                                <th style="width: 90px;"><span>Đơn vị</span></th>
                                <th style="width: 20px;"><span></span></th>
                            </tr>
                        </thead>
                        <tbody id="insertMachineRow">
                            @if (Model.BOMMachine_DetailsFormModels != null)
                            {
                                foreach (var item in Model.BOMMachine_DetailsFormModels)
                                {
                                <tr>
                                    <td>
                                        @Html.DropDownList("MachineID", new SelectList(ViewData["Machine"] as IEnumerable<Data.Models.Machine>, "ID", "Name", item.MachineID), "Chọn máy móc", new { @class = "span2 col-sm-1 form-control accesscorychange" })
                                    </td>
                                    <td>
                                        <input type="text" id="Quantity" name="Quantity" class="numeric text-right qty checkvalid" value="@item.Quantity" >
                                    </td>
                                    <td>
                                        <input type="text" id="Detail" name="Detail" class="txt1 description" value="@item.Description">
                                    </td>
                                    <td>
                                        @Html.DropDownList("UnitID", new SelectList(ViewData["UOM"] as IEnumerable<Data.Models.UOM>, "ID", "Name", item.UnitID), "Chọn đơn vị", new { @class = "span2 col-sm-1 form-control" })
                                    </td>
                                    <td>
                                        <div class="remove" id="delRow" onclick="RemoveRow(this);"></div>
                                    </td>
                                </tr>
                                }
                            }
                            else
                            {
                                <tr>
                                    <td>
                                        @Html.DropDownList("MachineID", new SelectList(ViewData["Machine"] as IEnumerable<Data.Models.Machine>, "ID", "Name"), "Chọn máy móc", new { @class = "span2 col-sm-1 form-control accesscorychange" })
                                    </td>
                                    <td>
                                        <input type="text" id="Quantity" name="Quantity" class="numeric text-right qty checkvalid">
                                    </td>
                                    <td>
                                        <input type="text" id="Detail" name="Detail" class="txt1 description">
                                    </td>
                                    <td>
                                        @Html.DropDownList("UnitID", new SelectList(ViewData["UOM"] as IEnumerable<Data.Models.UOM>, "ID", "Name"), "Chọn đơn vị", new { @class = "span2 col-sm-1 form-control accesscorychange" })
                                    </td>
                                    <td>
                                        <div class="remove" id="delRow" onclick="RemoveRow(this);"></div>
                                    </td>
                                </tr>
                            }

                        </tbody>
                    </table>
                </div>
                <div class="tablefooter">
                    <div class="pos1">
                        <input type="button" id="addline_CreateMachine" class="addline" value="Thêm máy móc">
                    </div>
                </div>
                <div class="cleardiv"></div>
                <div class="btn-group">
                    <div class="btn-group">
                        <button id="btnSaveSale" type="submit" class="btn btn-defaultxero">Save</button>
                    </div>
                </div>
                <div class="btn-group">
                    <button id="btnCancelSale" type="button" onclick="javascript: location.href = '/BOM';" class="btn btn-defaultxero">Cancel</button>
                </div>
            </div>
        </div>

    </form>
}
else
{ 
    <form class="form-horizontal" role="form" id="CheckValid">
        <input type="hidden" id="ID" name="ID" value="0" />
        <div class="form-group">
            <label for="Code" class="col-sm-2 control-label">Mã</label>
            <div class="col-sm-6">
                <input type="text" class="form-control" id="Code" name="Code" placeholder="Code">
            </div>
        </div>
        <div class="form-group">
            <label for="Name" class="col-sm-2 control-label">Tên</label>
            <div class="col-sm-6">
                <input type="text" class="form-control" id="Name" name="Name" placeholder="Name">
            </div>
        </div>
        <div class="form-group">
            <label for="dtEfective" class="col-sm-2 control-label">Ngày tác động</label>
            <div class="col-sm-6">
                <div class='input-group date' id='dtEfective'>
                    <input type='text' class="form-control" />
                    <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span>
                    </span>
                </div>

            </div>
        </div>
        <div class="form-group">
            <label for="ProductTypeID" class="col-sm-2 control-label">Sản phẩm</label>
            <div class="col-sm-6">
                @Html.DropDownList("ProductID", new SelectList(ViewData["Product"] as IEnumerable<Data.Models.Product>, "ID", "Name"), "Chọn sản phẩm", new { @class = "form-control" })
            </div>
        </div>
        <div class="form-group">
            <label for="Color" class="col-sm-2 control-label">Màu sắc</label>
            <div class="col-sm-6">
                <input type="text" class="form-control" id="Color" name="Color" placeholder="Color">
            </div>
        </div>
        <div class="form-group">
            <label for="Description" class="col-sm-2 control-label">Mô tả</label>
            <div class="col-sm-6">
                <textarea id="Description" name="Description" class="form-control" rows="5" placeholder="Mô tả"></textarea>
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-6">
                <div class="checkbox">
                    <label>
                        <input type="checkbox" id="Active" name="Active">
                        Active
                    </label>
                </div>
            </div>
        </div>
        <div class="form-group">
            <div class="c_container col-sm-10 col-sm-offset-1">
                <div id="form_body">
                    <table id="tblSaleDetail">
                        <thead>
                            <tr>
                                <th style="width: 140px;"><span>Nguyên liệu</span></th>
                                <th style="width: 170px;"><span>Mô tả</span></th>
                                <th style="width: 80px;"><span>Số lượng</span></th>
                                <th style="width: 90px;"><span>Đơn vị</span></th>
                                <th style="width: 20px;"><span></span></th>
                            </tr>
                        </thead>
                        <tbody id="insertRow">
                            <tr>
                                <td>
                                    @Html.DropDownList("MaterialID", new SelectList(ViewData["Material"] as IEnumerable<Data.Models.Material>, "ID", "Name"), "Chọn nguyên liệu", new { @class = "span2 col-sm-1 form-control materialchange" })
                                </td>
                                <td>
                                    <input type="text" id="Detail" name="Detail" class="txt1 description">
                                </td>
                                <td>
                                    <input type="text" id="Quantity" name="Quantity" class="numeric text-right qty checkvalid">
                                </td>
                                <td>
                                    @Html.DropDownList("UnitID", new SelectList(ViewData["UOM"] as IEnumerable<Data.Models.UOM>, "ID", "Name"), "Chọn đơn vị", new { @class = "span2 col-sm-1 form-control" })
                                </td>
                                <td>
                                    <div class="remove" id="delRow" onclick="RemoveRow(this);"></div>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
                <div class="tablefooter">
                    <div class="pos1">
                        <input type="button" id="addline_CreateSale" class="addline" value="Thêm công thức chi tiết">
                    </div>
                </div>
                <div class="cleardiv"></div>
                <div id="form_body">
                    <table id="tblAccesoryDetail">
                        <thead>
                            <tr>
                                <th style="width: 140px;"><span>Phụ kiện</span></th>
                                <th style="width: 80px;"><span>Số lượng</span></th>
                                <th style="width: 170px;"><span>Mô tả</span></th>
                                <th style="width: 90px;"><span>Đơn vị</span></th>
                                <th style="width: 90px;"><span>Giá</span></th>
                                <th style="width: 20px;"><span></span></th>
                            </tr>
                        </thead>
                        <tbody id="insertAccessRow">
                            <tr>
                                <td>
                                    @Html.DropDownList("AccessorieID", new SelectList(ViewData["Accessory"] as IEnumerable<Data.Models.Accessory>, "ID", "Name"), "Chọn phụ kiện", new { @class = "span2 col-sm-1 form-control accesscorychange" })
                                </td>
                                <td>
                                    <input type="text" id="QuantityAcces" name="QuantityAcces" class="numeric text-right qty checkvalid">
                                </td>
                                <td>
                                    <input type="text" id="DetailAcces" name="DetailAcces" class="txt1 description">
                                </td>
                                <td>
                                    @Html.DropDownList("UnitIDAcces", new SelectList(ViewData["UOM"] as IEnumerable<Data.Models.UOM>, "ID", "Name"), "Chọn đơn vị", new { @class = "span2 col-sm-1 form-control accesscorychange" })
                                </td>
                                 <td>
                                    <input type="text" id="PriceAcces" name="PriceAcces" class="numeric text-right qty checkvalid">
                                </td>
                                <td>
                                    <div class="remove" id="delRow" onclick="RemoveRow(this);"></div>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
                <div class="tablefooter">
                    <div class="pos1">
                        <input type="button" id="addline_CreateAccsess" class="addline" value="Thêm phụ kiện">
                    </div>
                </div>
                <div class="cleardiv"></div>
                <div id="form_body">
                    <table id="tblWorkerDetail">
                        <thead>
                            <tr>
                                <th style="width: 140px;"><span>Nhân công</span></th>
                                <th style="width: 80px;"><span>Số lượng</span></th>
                                <th style="width: 170px;"><span>Mô tả</span></th>
                                <th style="width: 90px;"><span>Đơn vị</span></th>
                                <th style="width: 20px;"><span></span></th>
                            </tr>
                        </thead>
                        <tbody id="insertWorkerRow">
                            <tr>
                                <td>
                                    @Html.DropDownList("WorkerLevelID", new SelectList(ViewData["Worker"] as IEnumerable<Data.Models.WorkerLevel>, "ID", "Name"), "Chọn nhân công", new { @class = "span2 col-sm-1 form-control accesscorychange" })
                                </td>
                                <td>
                                    <input type="text" id="Quantity" name="Quantity" class="numeric text-right qty checkvalid">
                                </td>
                                <td>
                                    <input type="text" id="Detail" name="Detail" class="txt1 description">
                                </td>
                                <td>
                                    @Html.DropDownList("UnitID", new SelectList(ViewData["UOM"] as IEnumerable<Data.Models.UOM>, "ID", "Name"), "Chọn đơn vị", new { @class = "span2 col-sm-1 form-control accesscorychange" })
                                </td>
                                <td>
                                    <div class="remove" id="delRow" onclick="RemoveRow(this);"></div>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
                <div class="tablefooter">
                    <div class="pos1">
                        <input type="button" id="addline_CreateWorker" class="addline" value="Thêm nhân công">
                    </div>
                </div>
                <div class="cleardiv"></div>
                <div id="form_body">
                    <table id="tblMachineDetail">
                        <thead>
                            <tr>
                                <th style="width: 140px;"><span>Máy móc</span></th>
                                <th style="width: 80px;"><span>Số lượng</span></th>
                                <th style="width: 170px;"><span>Mô tả</span></th>
                                <th style="width: 90px;"><span>Đơn vị</span></th>
                                <th style="width: 20px;"><span></span></th>
                            </tr>
                        </thead>
                        <tbody id="insertMachineRow">
                            <tr>
                                <td>
                                    @Html.DropDownList("MachineID", new SelectList(ViewData["Machine"] as IEnumerable<Data.Models.Machine>, "ID", "Name"), "Chọn máy móc", new { @class = "span2 col-sm-1 form-control accesscorychange" })
                                </td>
                                <td>
                                    <input type="text" id="Quantity" name="Quantity" class="numeric text-right qty checkvalid">
                                </td>
                                <td>
                                    <input type="text" id="Detail" name="Detail" class="txt1 description">
                                </td>
                                <td>
                                    @Html.DropDownList("UnitID", new SelectList(ViewData["UOM"] as IEnumerable<Data.Models.UOM>, "ID", "Name"), "Chọn đơn vị", new { @class = "span2 col-sm-1 form-control accesscorychange" })
                                </td>
                                <td>
                                    <div class="remove" id="delRow" onclick="RemoveRow(this);"></div>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
                <div class="tablefooter">
                    <div class="pos1">
                        <input type="button" id="addline_CreateMachine" class="addline" value="Thêm máy móc">
                    </div>
                </div>
                <div class="cleardiv"></div>
                <div class="btn-group">
                    <div class="btn-group">
                        <button id="btnSaveSale" type="submit" class="btn btn-defaultxero">Save</button>
                    </div>
                </div>
                <div class="btn-group">
                    <button id="btnCancelSale" type="button" onclick="javascript: location.href = '/BOM';" class="btn btn-defaultxero">Cancel</button>
                </div>
                <div class="cleardiv"></div>
            </div>
        </div>
    </form>
}
<script type="text/javascript">
    var $frt;
    function RemoveRow(element) {
        var rowCount = $('#tblSaleDetail tbody tr').length;
        if (rowCount < 1) {
            return;
        }
        else {
            var tr = $(element).closest('tr');
            tr.remove();
        }
    }
    $("#addline_CreateSale").click(function () {
        addTableRow(tblMachineDetail);
        return false;
    });
    $("#addline_CreateMachine").click(function () {
        addMachineRow();
        return false;
    });
    $("#addline_CreateWorker").click(function () {
        addWorkerRow();
        return false;
    });
    $("#addline_CreateAccsess").click(function () {
        addAccesRow();
        return false;
    });
    function addMachineRow() {
        var $tr = jQuery('div#form_body table#tblMachineDetail').find("tbody tr:last").clone(true);
        if (typeof ($tr.html()) == "undefined") {
            jQuery('div#form_body table#tblMachineDetail tbody#insertRow').html($frt);
            //jQuery('div#form_body table#tblSaleDetail').find("tbody tr:last").after($frt);
        }
        else {
            jQuery('div#form_body table#tblMachineDetail').find("tbody tr:last").after($tr);
        }

        $('.qty', jQuery('div#form_body table#tblMachineDetail').find("tbody tr:last")).val("");
        $('.description', jQuery('div#form_body table#tblMachineDetail').find("tbody tr:last")).val("");

    };
    function addWorkerRow() {
        var $tr = jQuery('div#form_body table#tblWorkerDetail').find("tbody tr:last").clone(true);
        if (typeof ($tr.html()) == "undefined") {
            jQuery('div#form_body table#tblWorkerDetail tbody#insertRow').html($frt);
            //jQuery('div#form_body table#tblSaleDetail').find("tbody tr:last").after($frt);
        }
        else {
            jQuery('div#form_body table#tblWorkerDetail').find("tbody tr:last").after($tr);
        }

        $('.qty', jQuery('div#form_body table#tblWorkerDetail').find("tbody tr:last")).val("");
        $('.description', jQuery('div#form_body table#tblWorkerDetail').find("tbody tr:last")).val("");

    };
    function addAccesRow() {
        var $tr = jQuery('div#form_body table#tblAccesoryDetail').find("tbody tr:last").clone(true);
        if (typeof ($tr.html()) == "undefined") {
            jQuery('div#form_body table#tblAccesoryDetail tbody#insertRow').html($frtAcces);
            //jQuery('div#form_body table#tblSaleDetail').find("tbody tr:last").after($frt);
        }
        else {
            jQuery('div#form_body table#tblAccesoryDetail').find("tbody tr:last").after($tr);
        }

        $('.qty', jQuery('div#form_body table#tblAccesoryDetail').find("tbody tr:last")).val("");
        $('.description', jQuery('div#form_body table#tblAccesoryDetail').find("tbody tr:last")).val("");

    };
    function addTableRow() {
        var $tr = jQuery('div#form_body table#tblSaleDetail').find("tbody tr:last").clone(true);
        if (typeof ($tr.html()) == "undefined") {
            jQuery('div#form_body table#tblSaleDetail tbody#insertRow').html($frt);
            //jQuery('div#form_body table#tblSaleDetail').find("tbody tr:last").after($frt);
        }
        else {
            jQuery('div#form_body table#tblSaleDetail').find("tbody tr:last").after($tr);
        }

        $('.qty', jQuery('div#form_body table#tblSaleDetail').find("tbody tr:last")).val("");
        $('.description', jQuery('div#form_body table#tblSaleDetail').find("tbody tr:last")).val("");

    };
    $(document).ready(function () {
        $frt = jQuery('div#form_body table#tblSaleDetail').find("tbody tr:last").clone(true);
        $frtAcces = jQuery('div#form_body table#tblAccesoryDetail').find("tbody tr:last").clone(true);
        $(".materialchange").change(function () {
            ChangeProduct(this);
        });
        $(".accesscorychange").change(function () {
            ChangeAcces(this);
        });
    });
    function ChangeProduct(element) {
        //
        var good = $(element).val();
        var tbody = $(element).closest('tr');
        var goodID = $(".materialchange", tbody).val();
        if (goodID != "") {
            if (goodID > 0) {
                $.ajax({
                    type: "GET",
                    url: '@Url.Action("GetMaterial", "BOM")',
                    data: {
                        ID: goodID
                    },
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        try {
                            $('.description', tbody).val(data.Description);
                            $('.qty', tbody).val("1");
                        } catch (ex) { }
                    }
                }).error(function (event, jqXHR, ajaxSettings, thrownError) {
                    alert('Error when loading data');
                });
            }
            else if (goodID == 0) {
                //alert("Add new inventory");
            }
        }
        else {

        }
    }
    function ChangeAcces(element) {
        //
        var good = $(element).val();
        var tbody = $(element).closest('tr');
        var goodID = $(".accesscorychange", tbody).val();
        if (goodID != "") {
            if (goodID > 0) {
                $.ajax({
                    type: "GET",
                    url: '@Url.Action("GetAcces", "BOM")',
                    data: {
                        ID: goodID
                    },
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        try {
                            $('.description', tbody).val(data.Description);
                            $('.qty', tbody).val("1");
                        } catch (ex) { }
                    }
                }).error(function (event, jqXHR, ajaxSettings, thrownError) {
                    alert('Error when loading data');
                });
            }
            else if (goodID == 0) {
                //alert("Add new inventory");
            }
        }
        else {

        }
    }
</script>
